#coding=utf-8
#@Time:2020/3/23 3:20 PM
#@Author:希哥
#@File:fix_otaOrder.py

import sql
import requests
import re
import sys
import dingding

reload(sys)
sys.setdefaultencoding('utf8')
s = requests.session()

'''
输入OTA订单号，进行退款操作，可以同时处理多个订单
'''
def fix(operator, type, *args):
    if type == 'None':
        return '请选择退款方式'
    for i in range(0,len(args)):
        import time
        now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))  # 数据产生日期
        order_no = args[i]
        if order_no == '':
            return '请输入订单号'
        else:
            updata_01_sql = "UPDATE order_to_ota SET refund_status = 'fail' WHERE order_id in (SELECT id FROM orders WHERE order_no = '"+order_no+"');"
            updata_02_sql = "UPDATE order_to_ota SET refund_status = NULL WHERE order_id in (SELECT id FROM orders WHERE order_no = '" + order_no + "');"
            order_id_sql = "SELECT id FROM orders WHERE order_no = '"+order_no+"';"
            '''
            修改order_to_ota表中的退款状态为fail
            '''
            sql.update(updata_01_sql,'hupo_online')
            '''
            通过订单号查找订单ID
            '''
            order_id = sql.search(order_id_sql,'hupo_online')
            # data = {'token': '8ce946af3c754010bb8b7332e69dbfd8', 'id': str(order_id)}  #测试环境
            data = {'token':'ed343bca1a1d4a39bd7646431fab9af0','id':str(order_id)}
            # fix_ota_order = s.post('https://hp-mall-admin.hupovip.cn/hp-mall-admin/v1/order/ota/refundToUser', data=data)  #测试环境
            '''
                仅退款给用户
            '''
            if type == 'refund_to_user':
                fix_ota_order = s.post('https://mall-admin.hupovip.cn/hp-mall-admin/v1/order/ota/refundToUser',data=data)
                '''
                修改order_to_ota表中的退款状态为null
                '''
                sql.update(updata_02_sql,'hupo_online')
                msg = '任务提示：' + str(now) + '，OTA退款给用户，订单号：' + order_no + str(fix_ota_order.text) + '，操作人：' + str(operator)
            '''
                申请ota退款同时退款给用户
            '''
            if type == 'refund_to_ota':
                fix_ota_order = s.post('https://mall-admin.hupovip.cn/hp-mall-admin/v1/order/ota/refundToOta',data=data)
                fix_user_order = s.post('https://mall-admin.hupovip.cn/hp-mall-admin/v1/order/ota/refundToUser',data=data)
                msg = '任务提示：' + str(now) + '，申请OTA退款并且同时退款给用户，订单号：' + order_no + '，OTA退款状态：' + str(fix_ota_order.text) + '，退款给用户状态：' + str(fix_user_order.text) + '，操作人：' + str(operator)
            '''
                仅申请ota退款
            '''
            if type == 'refund_to_onlyota':
                fix_ota_order = s.post('https://mall-admin.hupovip.cn/hp-mall-admin/v1/order/ota/refundToOta',data=data)
                msg = '任务提示：' + str(now) + '，申请OTA退款，订单号：' + order_no + '，OTA退款状态：' + str(fix_ota_order.text) + '，操作人：' + str(operator)

            write_sql = "insert into xige_hp_test.order_fix_record(create_time,order_no,operator_type,operator,result) values('%s','%s','%s','%s','%s')" % (now, order_no, 'ota订单退款', operator, msg)
            sql.insert(write_sql, 'hupo_online')
            '''
                钉钉消息通知
            '''
            dingding.DingTalk_Base().send_msg(msg)
            return msg

'''
    去掉字符串中的emoji
'''
def filter_emoji(text,restr=''):
    try:
        text = unicode(text, "utf-8")
    except TypeError as e:
        pass
    try:
        #highpoints = re.compile(u'[\U00010000-\U0010ffff]')
        highpoints = re.compile(u'['u'\U0001F300-\U0001F64F' u'\U0001F680-\U0001F6FF'u'\u2600-\u2B55]+')
    except re.error:
        # highpoints = re.compile(u'[\uD800-\uDBFF][\uDC00-\uDFFF]')
        highpoints = re.compile(u'('u'\ud83c[\udf00-\udfff]|'u'\ud83d[\udc00-\ude4f\ude80-\udeff]|'u'[\u2600-\u2B55])+')
    result = highpoints.sub(u'', text).encode('utf-8')
    new_result = result.strip().replace('&','').replace('$','').replace('^','').replace('¥','').replace('~','').replace('～','').replace('?','').replace('!','')
    return new_result

'''
    去掉特殊字符后再次下单
'''
def fix_username(operator, order_no):
    import time
    now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))  # 数据产生日期
    if order_no == '':
        return '请输入订单号'
    else:
        order_id_sql = "SELECT id FROM orders WHERE order_no = '"+ order_no + "'"
        order_id = sql.search(order_id_sql, 'hupo_online')
        username_sql = "SELECT u.name FROM orders as o LEFT JOIN `user` as u ON o.user_id = u.id WHERE o.order_no = '"+ order_no +"'"
        mobile_sql = "SELECT u.mobile FROM orders as o LEFT JOIN `user` as u ON o.user_id = u.id WHERE o.order_no = '" + order_no + "'"
        username = sql.search(username_sql, 'hupo_online')
        mobile = sql.search(mobile_sql, 'hupo_online')
        if username == 'None':
            new_username = '用户' + mobile
            update_username_sql = "UPDATE `user` SET `name` = '" + new_username + "' WHERE mobile = '" + mobile + "'"
            sql.update(update_username_sql, 'hupo_online')
        if username == '':
            new_username = '用户' + mobile
            update_username_sql = "UPDATE `user` SET `name` = '" + new_username + "' WHERE mobile = '" + mobile + "'"
            sql.update(update_username_sql, 'hupo_online')
        else:
            '''
                去除username中的特殊字符
            '''
            new_username = filter_emoji(username)
            update_username_sql = "UPDATE `user` SET `name` = '"+ new_username +"' WHERE mobile = '"+ mobile +"'"
            sql.update(update_username_sql, 'hupo_online')
        '''
            申请ota再次下单
        '''
        data = {'token': 'ed343bca1a1d4a39bd7646431fab9af0', 'id': str(order_id)}
        #data = {'token': '8ce946af3c754010bb8b7332e69dbfd8', 'id': str(order_id)}  #测试环境
        #fix_ota_order = s.post('https://hp-mall-admin.hupovip.cn/hp-mall-admin/v1/order/ota/reDoOrderToOta', data=data)  #测试环境
        fix_ota_order = s.post('https://mall-admin.hupovip.cn/hp-mall-admin/v1/order/ota/reDoOrderToOta', data=data)

        '''
            写入记录
        '''
        write_sql = "insert into xige_hp_test.order_fix_record(create_time,order_no,operator_type,operator,result) values('%s','%s','%s','%s','%s')" % (
        now, order_no, '修复昵称原因ota下单失败', operator, fix_ota_order.text)
        sql.insert(write_sql, 'hupo_online')
        '''
            钉钉消息通知
        '''
        msg = '任务提示：' + str(now) + '，订单号：' + order_no + '修改用户昵称再次下单,'+ str(fix_ota_order.text) +'，操作人：' + str(operator)
        dingding.DingTalk_Base().send_msg(msg)
        return fix_ota_order.text

if __name__=='__main__':
    # fix_username('希哥','202004130300001253')
    print filter_emoji('用户！！！～～～?17781108472')
